/**
 ******************************************************************************
 * @file    print_init.c
 * @author  hyseim software Team
 * @date    01-Sep-2023
 * @brief   This file provides all the printInit functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "common.h"
#include "uart.h"
#include "pad.h"

void print_str(char *str)
{
    while (*str)
        putc_uart(*str++);
}

volatile const unsigned int * pprint_UartLsr = NULL;
volatile unsigned int * pprint_UartThr = NULL;
volatile const unsigned int * pprint_UartRbr = NULL;

UART_t *printUart = UART0;

#if USE_PLL_CLK
uint32_t uartClk = 75000000;
#else
uint32_t uartClk = 24000000;
#endif

void print_init(void)
{
#if USE_PLL_CLK
    // switch assi clk to PLL clk
    reg32(CRG_BASE + 0x03c) = 1 << 8 | 1 << 24;
    reg32(CRG_BASE + 0x03c) = 0 << 11 | 1 << 27; // ASSI_CLK=75M
    reg32(CRG_BASE + 0x044) = 0x80000000;
#endif
    UART_Init_t UART_InitStruct;
    UART_StructInit(&UART_InitStruct);

    switch ((int32_t)printUart)
    {
    case (int32_t)UART0:
        PAD_XIOMUX(GPIOB2_MTCLKB_XIO34_CMP_IN7, XIO_ASSI0_IO1);
        PAD_XIOMUX(GPIOC4_XIO68_ADC0_SE3, XIO_ASSI0_IO2);
        ASSI_DeInit(UART0);
        UART_Init(printUart, uartClk, &UART_InitStruct);
        ASSI_Init(UART0);
        break;
    case (int32_t)UART1:
        PAD_XIOMUX(GPIOD2_TRGMUXOUT3_XIO98_ADC0_SE0_CMP_IN0, XIO_ASSI0_IO1);
        PAD_XIOMUX(GPIOD3_XIO99, XIO_ASSI0_IO2);
        ASSI_DeInit(UART1);
        UART_Init(printUart, uartClk, &UART_InitStruct);
        ASSI_Init(UART1);
        break;
    default:
        break;
    }
    pprint_UartLsr = (volatile const unsigned int *)&(printUart->LSR);
    pprint_UartThr = (volatile unsigned int *)&(printUart->THR);
    pprint_UartRbr = (volatile const unsigned int *)&(printUart->RBR);
}
